<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在线状态" prop="isOnLine">
        <el-select
          v-model="queryParams.isOnLine"
          placeholder="在线状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table  v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="imei_sn" label="设备编号"></el-table-column>
      <el-table-column prop="truename" label="姓名" :formatter="caseNamesFormat">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleinfoname(scope.row.imei_sn)">{{ caseNamesFormat(scope.row.truename) }}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="blood_upload" label="血压上报间隔（秒）">
        <template slot-scope="scope">
          <el-link type="primary" @click="handlexueya(scope.row.imei_sn)">{{ scope.row.blood_upload }}</el-link>
        </template>
      </el-table-column> -->
      <el-table-column prop="blood_upload" label="血压上报间隔（秒）"></el-table-column>
      <el-table-column prop="heart_upload" label="心率上报间隔（秒）">
        <template slot-scope="scope">
          <el-link type="primary" @click="handlexinlv(scope.row.imei_sn)">{{ scope.row.heart_upload }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="temperature_upload" label="体温上报间隔（秒）">
        <template slot-scope="scope">
          <el-link type="primary" @click="handletiwen(scope.row.imei_sn)">{{ scope.row.temperature_upload }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="is_online" label="是否在线" :formatter="caseLineFormat"></el-table-column>
      <el-table-column prop="power_rate" label="剩余电量"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.imei_sn)"
            v-hasPermi="['system:role:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

     <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body @close="closeinfo">
      <div class="shebeiinfo" v-loading="loadinginfo">
        <span>心率为：{{ shebeiinfo.heart_rate }}，更新时间为：{{ shebeiinfo.blood_time }}</span>
        <el-divider></el-divider>
        <span>血压为：{{ shebeiinfo.max_rate }} / {{ shebeiinfo.min_rate }}，更新时间为：{{ shebeiinfo.heart_time }}</span>
        <el-divider></el-divider>
        <span>体温为：{{ shebeiinfo.temperature }}，更新时间为：{{ shebeiinfo.temperature_time }}</span>
      </div>
    </el-dialog>
    <!-- <div id="heihei"></div> -->
  </div>
</template>

<script>
import { listEquipment, nameUpdate, temperatureUpdate, booldUpdate, heartUpdate, shebeiDetail } from "@/api/bracelet/equipment";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      equipmentList: [],
      // 状态数据字典
      statusOptions: [
        {
          dictLabel:"在线",
          dictValue:"1"
        },
        {
          dictLabel:"离线",
          dictValue:"0"
        }
      ],
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      open: false,
      loadinginfo: true,
      title:'',
      // 非多个禁用
      multiple: true,
      // 查询参数
      queryParams: {
        limit: 10,
        page: 1
      },
      shebeiinfo: {
        blood_time: "",
        heart_rate: "",
        heart_time: "",
        max_rate: "",
        min_rate: "",
        temperature: "",
        temperature_time: "",
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // this.echt()
  },
  methods: {
    echt() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById('heihei'));
      // 绘制图表
      myChart.setOption({
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      });
    },
    getList() {
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows
        this.total = response.total
          this.loading = false
      }).catch(function(error) {
        console.log(error);
      });
    },
    caseNamesFormat(name) {
      // 姓名缺省值补全
      var showProp = null
      name ? showProp = name : showProp = '---'
      return showProp
    },
    caseLineFormat(row) {
      //在线状态
      var showProp = null
      if(row.is_online == "1"){
        showProp = '在线'
      }else {
        showProp = '离线'
      }
      return showProp
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    handleinfoname(id) {
      this.$prompt('请输入姓名', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        nameUpdate(id,value).then(response => {
          this.$message({
            type: 'success',
            message: '人员姓名绑定成功！'
          });
          this.getList();
        }).catch(function(error) {
          console.log(error);
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });       
      });
    },
    handlexueya(id) {
      this.$prompt('请输入血压上报间隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[0-9]*[1-9][0-9]*$/,
        inputErrorMessage: '只能输入数值'
      }).then(({ value }) => {
        booldUpdate(id,value).then(response => {
          this.$message({
            type: 'success',
            message: '血压上报间隔修改成功！'
          });
          this.getList();
        }).catch(function(error) {
          console.log(error);
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });       
      });
    },
    handlexinlv(id) {
      this.$prompt('请输入心率上报间隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[0-9]*[1-9][0-9]*$/,
        inputErrorMessage: '只能输入数值'
      }).then(({ value }) => {
        heartUpdate(id,value).then(response => {
          this.$message({
            type: 'success',
            message: '心率上报间隔修改成功！'
          });
          this.getList();
        }).catch(function(error) {
          console.log(error);
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });       
      });
    },
    handletiwen(id) {
      this.$prompt('请输入体温上报间隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[0-9]*[1-9][0-9]*$/,
        inputErrorMessage: '只能输入数值'
      }).then(({ value }) => {
        temperatureUpdate(id,value).then(response => {
          this.$message({
            type: 'success',
            message: '体温上报间隔修改成功！'
          });
          this.getList();
        }).catch(function(error) {
          console.log(error);
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });       
      });
    },
    handleUpdate(id) {
      this.open = true;
      this.title = '详情'
      shebeiDetail(id).then(response => {
        this.loadinginfo = false;
        this.shebeiinfo = response.data;
      }).catch(function(error) {
        console.log(error);
      });
    },
    closeinfo() {
      this.loadinginfo = true
    }
  }
}
</script>

<style scoped>
#heihei{
  width: 400px;
  height: 400px;
}
.shebeiinfo{
  text-align: center;
}
</style>